package com.xqls.biz.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xqls.bean.UserBean;
import com.xqls.biz.UserBiz;
import com.xqls.dal.model.User;
import com.xqls.service.UserService;
import com.xqls.util.CommonResult;
import com.xqls.util.ObjectUtil;

@Service
public class UserBizImpl implements UserBiz{

	private static Logger logger=Logger.getLogger(UserBiz.class);
	
	@Autowired private UserService userService;
	
	@Override
	public CommonResult insertUser(UserBean userBean) {
		CommonResult commonResult=new CommonResult();
		User user=null;
		try {
			user=(User) ObjectUtil.transfer(userBean, User.class);
			commonResult.setIsSuccess(this.userService.insertUser(user));
		} catch (Exception e) {
			logger.error("添加账户异常!", e);
		}
		return commonResult;
	}

	@Override
	public CommonResult updateUser(UserBean userBean) {
		CommonResult commonResult=new CommonResult();
		User user=null;
		List<User> userList=null;
		try {
			user=(User) ObjectUtil.transfer(userBean, User.class);
			userList=this.userService.getUserByUserName(user.getUserName());
			for(User getUser:userList){
				if(!getUser.getUserId().equals(user.getUserId()))
				{
					commonResult.setResultMsg("用户名重复");
					return commonResult;
				}
			}
			
			commonResult.setIsSuccess(this.userService.updateUser(user));
		} catch (Exception e) {
			logger.error("修改账户异常!", e);
		}
		return commonResult;
	}
	
	@Override
	public CommonResult updateUserSelective(UserBean userBean) {
		CommonResult commonResult=new CommonResult();
		User user=null;
		List<User> userList=null;
		try {
			user=(User) ObjectUtil.transfer(userBean, User.class);
			userList=this.userService.getUserByUserName(user.getUserName());
			for(User getUser:userList){
				if(!getUser.getUserId().equals(user.getUserId()))
				{
					commonResult.setResultMsg("用户名重复");
					return commonResult;
				}
			}
			
			commonResult.setIsSuccess(this.userService.updateUserSelective(user));
		} catch (Exception e) {
			logger.error("修改账户异常!", e);
		}
		return commonResult;
	}

	@Override
	public CommonResult deleteUser(String userId) {
		CommonResult commonResult=new CommonResult();
		try {
			commonResult.setIsSuccess(this.userService.deleteUser(userId));
		} catch (Exception e) {
			logger.error("删除账户异常!", e);
		}
		return commonResult;
	}

	@Override
	public User checkUserForLogin(String userName, String password) {
		User user=null;
		List<User> userList = null;
		try {
			userList = this.userService.getUserListForLogin(userName, password);
			if(!userList.isEmpty())
			{
				user=userList.get(0);
			}
		} catch (Exception e) {
			logger.error("查询账户异常!", e);
		}
		
		return user;
	}

	@Override
	public CommonResult getUserByKey(String userId) {
		CommonResult commonResult=new CommonResult();
		List<User> userList = null;
		try {
			userList = this.userService.getUserByKey(userId);
			if(!userList.isEmpty())
			{
                commonResult.setResultMsg("账户编号重复！");
			}
			else
			{
				commonResult.setIsSuccess(true);
			}
		} catch (Exception e) {
			logger.error("查询账户异常!", e);
		}
		return commonResult;
	}

	@Override
	public CommonResult getUserByUserName(String userName) {
		CommonResult commonResult=new CommonResult();
		List<User> userList = null;
		try {
			userList = this.userService.getUserByUserName(userName);
			if(!userList.isEmpty())
			{
				commonResult.setIsSuccess(true);
			}
		} catch (Exception e) {
			logger.error("查询账户异常!", e);
		}
		return commonResult;
	}

	@Override
	public UserBean getUserByName(String userName) {
		UserBean userBean=null;
		List<User> userList=null;
		try {
			userList=this.userService.getUserByUserName(userName);
			if(!userList.isEmpty()){
				userBean=(UserBean) ObjectUtil.transfer(userList.get(0), UserBean.class);
			}
		} catch (Exception e) {
			logger.error("查询账户异常!", e);
		}
		return userBean;
	}
	
	@Override
	public List<User> getList() {
		List<User> userList=null;
		try {
			userList=this.userService.getList();
		} catch (Exception e) {
			logger.error("查询账户异常!", e);
		}
		return userList;
	}

	@Override
	public CommonResult checkUser(String userName, String password) {
		CommonResult commonResult=new CommonResult();
		try {
			if(this.userService.getUserByUserName(userName).size()>0)
			{
				commonResult.setResultMsg("密码错误！");
			}
			else
			{
				commonResult.setResultMsg("账户名错误！");
			}
		} catch (Exception e) {
      		logger.error("查询账户异常!", e);
		}
		
		return commonResult;
	}
}
